package com.lightioc.util;

import java.lang.reflect.Method;

public class ReflectionUtil {
	public static Method obtainGetMethod(Object o, String fieldName) throws SecurityException, NoSuchMethodException {
		String methodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
		return o.getClass().getMethod(methodName, new Class[]{});
	}
	
	public static Method obtainGetMethod(Object o, String fieldName, Class<?> valueType) throws SecurityException, NoSuchMethodException {
		String methodName = "set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
		return o.getClass().getMethod(methodName, new Class[]{valueType});
	}
	
	public static Object invokeGetMethod(Object o, String fieldName) {
		Object result;
		try {
			Method m = obtainGetMethod(o, fieldName);
			result = m.invoke(o, new Object[] {});
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}
}
